package cn.bluech.lib.utils;

import android.util.Log;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

public class RunTimeUtil {
	private static boolean isRooted = false;

	// 判断机器Android是否已经root，即是否获取root权限
	public static boolean haveRoot() {
		if (!isRooted) {
			String ret = rootExec("echo test"); // 通过执行测试命令来检测
			if (ret != null) {
				isRooted = true;
			}
		}
		return isRooted;
	}

	public static String exec(String cmd) {
		String result = "";
		DataOutputStream dos = null;
		DataInputStream dis = null;

		try {
			Process p = Runtime.getRuntime().exec(cmd + "\n");
			dos = new DataOutputStream(p.getOutputStream());
			dis = new DataInputStream(p.getInputStream());

			dos.flush();
			dos.writeBytes("exit\n");
			dos.flush();
			String line = null;
			while ((line = dis.readLine()) != null) {
				Log.d("result", line);
				result += line;
			}
			p.waitFor();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		} finally {
			if (dos != null) {
				try {
					dos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (dis != null) {
				try {
					dis.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}

		return result;
	}

	// 执行命令但不关注结果输出
	public static int execSilent(String cmd) {
		int result = -1;
		DataOutputStream dos = null;

		try {
			Process p = Runtime.getRuntime().exec(cmd + "\n");
			dos = new DataOutputStream(p.getOutputStream());

			dos.flush();
			dos.writeBytes("exit\n");
			dos.flush();
			p.waitFor();
			result = p.exitValue();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (dos != null) {
				try {
					dos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}

		return result;
	}

	
	// 执行命令并且输出结果
	public static String rootExec(String cmd) {
		StringBuilder result = new StringBuilder();
		DataOutputStream dos = null;
		DataInputStream dis = null;
		
		try {
			Process p = Runtime.getRuntime().exec("su");// 经过Root处理的android系统即有su命令
			dos = new DataOutputStream(p.getOutputStream());
			dis = new DataInputStream(p.getInputStream());

			dos.writeBytes(cmd + "\n");
			dos.flush();
			dos.writeBytes("exit\n");
			dos.flush();
			String line = null;
			while ((line = dis.readLine()) != null) {
				Log.d("result", line);
				result.append(line);
			}
			p.waitFor();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		} finally {
			StreamUtils.close(dos);
			StreamUtils.close(dis);
		}

		return result.toString();
	}

	// 执行命令但不关注结果输出
	public static int rootExecSilent(String cmd) {
		int result = -1;
		DataOutputStream dos = null;
		
		try {
			Process p = Runtime.getRuntime().exec("su");
			dos = new DataOutputStream(p.getOutputStream());
			
			dos.writeBytes(cmd + "\n");
			dos.flush();
			dos.writeBytes("exit\n");
			dos.flush();
			p.waitFor();
			result = p.exitValue();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			StreamUtils.close(dos);
		}

		return result;
	}
}
